package be.kuleuven.peno3.mobiletoledo.view;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.Data.UserDAO;
import be.kuleuven.peno3.mobiletoledo.view.campusnetlogin.CampusnetLoginView;
import be.kuleuven.peno3.mobiletoledo.view.course.CourseNavigationView;
import be.kuleuven.peno3.mobiletoledo.view.messages.List;
import be.kuleuven.peno3.mobiletoledo.view.queue.QueueView;

public class MainMenuView extends Activity {
	private static final int CHANGE_SETTINGS_MENUITEM = 0;
	
	/**
	 * Initialize the activity, add menuitems to list.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		setContentView(R.layout.splash);
		ImageView splash=(ImageView) findViewById(R.id.splash);
		splash.setImageDrawable(this.getResources().getDrawable(R.drawable.splash));
		
        int delay = 1500; //Duration for splash screen to display
        final Handler handler = new Handler(); 
        Timer t = new Timer(); 
        t.schedule(new TimerTask() { 
        	public void run() { 
        		handler.post(new Runnable() { 
        			public void run() { 
        				displayRegistration(); 
        			} 
        		}); 
        	} 
        }, delay);
	}
	
	public void displayRegistration() {
		UserDAO userManager=new UserDAO(this);
		userManager.open();
		boolean userDataRead=userManager.readUserData();
		userManager.close();
		if(!userDataRead) {
			Intent i = new Intent(this, InsertUserData.class);
			startActivity(i);
		}
		displayMainMenu();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		try {
			UserDAO userManager=new UserDAO(this);
			userManager.open();
			userManager.readUserData();
			userManager.close();
		}
		catch(NullPointerException e) {
			e.printStackTrace();
		}
	}
	
	private void displayMainMenu(){
		setContentView(R.layout.mainmenu);
		
		TextView main_text = (TextView) findViewById(R.id.main_title);
		main_text.setText("MobileToledo");
		ImageView book=(ImageView) findViewById(R.id.book);
		book.setImageDrawable(this.getResources().getDrawable(R.drawable.book));
		ImageView calendar=(ImageView) findViewById(R.id.calendar);
		calendar.setImageDrawable(this.getResources().getDrawable(R.drawable.calendar));
		ImageView food=(ImageView) findViewById(R.id.food);
		food.setImageDrawable(this.getResources().getDrawable(R.drawable.food));
		ImageView login=(ImageView) findViewById(R.id.login);
		login.setImageDrawable(this.getResources().getDrawable(R.drawable.login));
		ImageView messages=(ImageView) findViewById(R.id.messages);
		messages.setImageDrawable(this.getResources().getDrawable(R.drawable.messages));
		ImageView question=(ImageView) findViewById(R.id.queue);
		question.setImageDrawable(this.getResources().getDrawable(R.drawable.queue));
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, CHANGE_SETTINGS_MENUITEM, 0, R.string.change_settings);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case CHANGE_SETTINGS_MENUITEM:
            Intent i = new Intent(this, InsertUserData.class);
            startActivity(i);
            return true;
        }
       
        return super.onMenuItemSelected(featureId, item);
    }
	
	/**
	 * Called when the user clicks on a menu-item.
	 * @param view The view that was clicked
	 */
	public void clickListener(View view) {
		ImageView image=(ImageView) view;
		if(image.getId()==R.id.calendar) {
			onClickAnimation(image);
			Intent i=new Intent(this, ScheduleView.class);
			activityStartDelay(i);
		}
		else if(image.getId()==R.id.login) {
			onClickAnimation(image);
			Intent i=new Intent(this, CampusnetLoginView.class);
			activityStartDelay(i);
		}
		else if(image.getId()==R.id.queue) {
			onClickAnimation(image);
			Intent i=new Intent(this, QueueView.class);
			activityStartDelay(i);
		}
		else if(image.getId()==R.id.food) {
			onClickAnimation(image);
			Intent i=new Intent(this, AlmaView.class);
			activityStartDelay(i);
		}
		else if(image.getId()==R.id.messages) {
			onClickAnimation(image);
			Intent i=new Intent(this, List.class);
			activityStartDelay(i);
		}
		else if(image.getId()==R.id.book) {
			onClickAnimation(image);
			Intent i=new Intent(this, CourseNavigationView.class);
			activityStartDelay(i);
		}
	}
	
	private void onClickAnimation(View view){
		view.startAnimation(AnimationUtils.loadAnimation(this, R.layout.image_click));
	}
	
	private void activityStartDelay(Intent intent){
		int delay = 400; //Time to delay activity startup
		final Intent i = intent;
		final Handler handler = new Handler(); 
        Timer t = new Timer(); 
        t.schedule(new TimerTask() { 
        	public void run() { 
        		handler.post(new Runnable() { 
        			public void run() { 
        				startActivity(i); 
        			} 
        		}); 
        	} 
        }, delay);
	}
}
